import { message } from "ant-design-vue";
import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkNameForm = async (_rule: dataSourceType = {}, value: any) => {
  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (value === "" || value.length === 0) {
      return Promise.reject(`${_rule.msg1}`);
    }
  }
  if (_rule.value && _rule.value == value) {
    // 没变化没校验的必要
    return Promise.resolve();
  }
  if (_rule.api && value) {
    // 没填值没有校验的必要
    var params = {
      name: value,
      id: _rule.id,
    };
    let res = await request[`${_rule.api}`](params);
    if (!res) {
      console.log(_rule.msg2, 7777)
      return Promise.reject(`${_rule.msg2}`);
    } else {
      return Promise.resolve();
    }
  }
};
export const ipz = async (
  _rule: dataSourceType,
  value: string,
  callback: () => {}
) => {
  // eslint-disable-next-line no-useless-escape
  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (value === "" || value.length === 0) {
      return Promise.reject(`${_rule.msg1}`);
    } else {
      const reg =
        /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
      return reg.test(value)
        ? Promise.resolve()
        : Promise.reject(`${_rule.msg2}`);
    }
  } else {
    return Promise.resolve();
  }
};
// 端口号验证,
export const portz = async (
  _rule: dataSourceType,
  value: string,
  callback: () => {}
) => {
  // eslint-disable-next-line no-useless-escape
  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (value === "" || value.length === 0) {
      return Promise.reject(`${_rule.msg1}`);
    } else {
      const reg =
        /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
      return reg.test(value)
        ? Promise.resolve()
        : Promise.reject(`${_rule.msg2}`);
    }
  } else {
    return Promise.resolve();
  }
};
// 域名验证,
export const httpz = async (
  _rule: dataSourceType,
  value: string,
  callback: () => {}
) => {
  // eslint-disable-next-line no-useless-escape
  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (value === "" || value.length === 0) {
      return Promise.reject(`${_rule.msg1}`);
    } else {
      const reg = /http[s]{0,1}:\/\/([\w.]+\/?)\S*/; //正则表达式
      return reg.test(value)
        ? Promise.resolve()
        : Promise.reject(`${_rule.msg2}`);
    }
  } else {
    return Promise.resolve();
  }
};
